﻿package banmuoi.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import banmuoi.entity.Category;
import banmuoi.util.HibernateUtil;

@WebServlet("/crudCategory.do")
public class CategoryAdmin extends SecurityAction {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String value = checkLogin(req, resp);
		if(value != null){
			if(value.equals("1") || value.equals("3")){//admin hoặc ProductManager		
				
				//LẤY THAM SỐ TỪ CÁC REQUEST
				String page = req.getParameter("page");
				String cateId = req.getParameter("categoryId");
				int categoryId = 0;
				try {
					categoryId = Integer.parseInt(cateId);
				} catch (Exception e) {
					System.out.println("Request Sai định dạng số! Không xử lý gì cả!");
				}
				
				Session session = HibernateUtil.openSession();
				
				//HIỆN DANH SÁCH
				String hql = "FROM Category";
				Query query = session.createQuery(hql);
				List<Category> entities = query.list();
				req.setAttribute("showCategory", entities);
				
				
				//REFRESH THỰC THỂ KHI ĐƯỢC GỌI TỚI
				if(categoryId != 0){
					Category entity = (Category) session.load(Category.class, categoryId);
					req.setAttribute("entityCategory", entity);
				}
	
				req.getRequestDispatcher("admin/CategoryAdmin.jsp").forward(req, resp);			
				session.close();
			}
		}
		
		else {
			req.getRequestDispatcher("admin/Login.jsp").forward(req, resp);
		}	
		
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String id = req.getParameter("txtId");
		String name = req.getParameter("txtName");
		String note = req.getParameter("txtNote");
		
		Session session = HibernateUtil.openSession();
		Transaction trans = session.beginTransaction();
		
		try{
			if(req.getParameter("btnCreate") != null){
				Category entity = new Category();
				
				entity.setName(name);
				entity.setNote(note);
				
				session.save(entity);
				req.setAttribute("error", "Inserted successfully !");
			}
			else if(req.getParameter("btnUpdate") != null){
				Category entity = new Category();
				
				entity.setId(Integer.valueOf(id));
				entity.setName(name);
				entity.setNote(note);
				
				session.update(entity);
				req.setAttribute("error", "Updated successfully !");
			}
			else if(req.getParameter("btnDelete") != null){
				Category entity = new Category(); 
				
				entity.setId(Integer.valueOf(id));
				
				session.delete(entity);
				req.setAttribute("error", "Deleted successfully !");
			}
			
			
			trans.commit();
		}
		catch (Exception e) {
			trans.rollback();
			req.setAttribute("error", e.getMessage());
		}
		
		session.close();
		this.doGet(req, resp);
	}
}
